<%@page import="java.util.Map.Entry"%>
<%@page import="java.util.Vector"%>
<%@page import="java.util.Hashtable"%>
<%@page import="utils.UnicodeUtils"%>
<%@page import="java.util.Map"%>
<%@page import="domain.BlogCommentFile"%>
<%@page import="java.util.Arrays"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="domain.BlogFile"%>
<%@page pageEncoding="UTF-8" %>
<%@page import="getools4me.UserManager"%>
<%@page import="java.util.List"%>
<%@page import="getools4me.BlogManager"%>
<%@page import="utils.DateUtils"%>
<%
	String blogID = request.getParameter("blogID");
	String blogCommentID = request.getParameter("blogCommentID");
	String fileID = null;
	
	String showCommentIDString = request.getParameter("showCommentIDString");
	
	boolean blogIDisOK = blogID!=null && blogID.matches("\\d+");
	boolean blogCommentIDisOK = blogCommentID!=null && blogCommentID.matches("\\d+");
	
	BlogFile file = null;
	Long id_pre = null;
	Long id_next = null;
	List<Long> list_commentIDs = null;
	Long lastModifiedDate = null;
	String title = null;
	byte[] content = null;
	String author = null;
	String email = null;
	
	if(blogIDisOK){
		file = BlogManager.getBlogFileById(Long.valueOf(blogID));
		fileID = blogID;
		if(file!=null){
			id_pre = BlogManager.getPreviousBlogID(file.getId());
			id_next = BlogManager.getNextBlogID(file.getId());
			list_commentIDs = BlogManager.getBlogCommentIDList(file.getId());
		}
	}else if(blogCommentIDisOK){
		file = BlogManager.getBlogCommentFileById(Long.valueOf(blogCommentID));
		fileID = blogCommentID;
	}
	
	if(file!=null){
		lastModifiedDate = file.getLastModifiedDate();
		title = file.getTitle();
		content = file.getContent();
		author = file.getAuthor();
		email = file.getEmail();
	}
	
	User user = UserManager.getCurrentUser();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="/blog/css/common.css" />
<link rel="stylesheet" type="text/css" href="/blog/css/menu.css" />
<link rel="stylesheet" type="text/css" href="/blog/css/detail.css" />
<script type="text/javascript" src="/blog/js/common.js"></script>

<title>Article Detail</title>
</head>
<body>

<div class="menu">
	<ul>
		<li><a href="/blog/page/home.jsp">Home</a></li>
		<li><a href="/blog/page/all.jsp">All</a></li>
		<li><%if(UserManager.isLogin()){ %><a href="<%=UserManager.getLogoutURL(request)%>">Logout</a><%}else{ %><a href="<%=UserManager.getLoginURL(request)%>">Login</a><%} %></li>
		<li><%if(UserManager.isAdmin()){ %>
		<a href="/blog/page/new.jsp">New</a><%} %>
		</li>
	</ul>
</div>


<%if(file==null){ %>
	<div class="center">
		<h1><%=fileID %>&nbsp;&lt;-- File with this id not exist!</h1>
	</div>
<%}else{ 
	// list the commnets
	Map<String, List<BlogCommentFile>> map_email_comment = new Hashtable<String, List<BlogCommentFile>>();
	if(list_commentIDs!=null){
		for(Long id : list_commentIDs){ 
			BlogCommentFile file_comment = BlogManager.getBlogCommentFileById(id);
			// if the comment file is null, then need to rebuild the comment id string
			if(file_comment==null) {
				BlogManager.reBuildBlogCommentIDString(file.getId());
				continue;
			}else{
				// add the commnet file to map
				String email_address = file_comment.getEmail()==null?null:UnicodeUtils.toStringFromUnicodeString(file_comment.getEmail());
				List<BlogCommentFile> list_comment = map_email_comment.get(email_address);
				if(list_comment==null){
					list_comment = new Vector<BlogCommentFile>();
				}
				list_comment.add(file_comment);
				map_email_comment.put(email_address,list_comment);
			}
		}
	}
%>


<div class="list">
	<div class="navigate">
		<%if(id_pre!=null){ %>
		<div class="left">
			<a href="/blog/page/detail.jsp?blogID=<%=id_pre%>">
			<span>←</span><span class="safeString title"><%=BlogManager.getBlogTitle(id_pre) %></span>
			</a>
		</div>
		<%} %>
		<%if(id_next!=null){ %>
			<div class="right">
			<a href="/blog/page/detail.jsp?blogID=<%=id_next%>">
				<span class="safeString title"><%=BlogManager.getBlogTitle(id_next) %></span><span>→</span>
			</a>
			</div>
		<%} %>
	</div>

	<div class="myArticle_outer">
	<span class="safeString article_author"><%=author%></span>
	<div class="myArticle">
			<div class="article_title">
				<span id="<%=fileID%>.title" class="safeString"><%=title%></span>
			</div>	
			<div class="article_body">
				<div class="article_original">
					<div class="article_timestamp">
						<span><%=DateUtils.format("<yy.MMM.dd",lastModifiedDate)%>&nbsp;&nbsp;<%=DateUtils.format("HH:mm>",lastModifiedDate)%></span>
					</div>
					<div class="article_content">
						<pre id="<%=fileID%>.content" class="safeString"><%=new String(file.getContent())%></pre>
					</div>
				</div>
				<%	List<BlogCommentFile> mineCommentFiles = map_email_comment.get(UserManager.getAdminEmail()); 
					if(mineCommentFiles!=null){
						for(BlogCommentFile bcf : mineCommentFiles){
							%>
							<div class="article_comment">
								<div class="article_timestamp">
									<span><%=DateUtils.format("<yy.MMM.dd",bcf.getLastModifiedDate())%>&nbsp;&nbsp;<%=DateUtils.format("HH:mm>",bcf.getLastModifiedDate())%></span>
								</div>
								<div class="article_content" id="blogCommentID_<%=bcf.getId()%>">
									<pre id="<%=bcf.getId()%>.content" class="safeString"><%=new String(bcf.getContent())%></pre>
								</div>
							</div>
							<%
						}
					}
				%>
			</div>
	</div>
	</div>
	<% // others' article
		map_email_comment.remove(UserManager.getAdminEmail());
		if(map_email_comment.size()>0){
			for(Entry<String, List<BlogCommentFile>> entry : map_email_comment.entrySet()){
				List<BlogCommentFile> list_commnets_other = entry.getValue();
			%>
				
			<div class="othersArticle">
				<div class="othersArticle_author">
					<span class="safeString article_author"><%=list_commnets_other.size()>0?list_commnets_other.get(0).getAuthor():""%></span>
				</div>	
				<div class="othersArticle_body">
				<%for(BlogCommentFile bcf : list_commnets_other){%>
					<div class="article_comment">
							<div class="article_timestamp">
								<span><%=DateUtils.format("<yy.MMM.dd",bcf.getLastModifiedDate())%>&nbsp;&nbsp;<%=DateUtils.format("HH:mm>",bcf.getLastModifiedDate())%></span>
							</div>
							<div class="article_content">
								<pre id="<%=bcf.getId()%>.content" class="safeString"><%=new String(bcf.getContent())%></pre>
							</div>
					</div>
				<%}%>
				</div>
			</div>
			<%}%>
		<%}%>

	
		<div class="commentIdString">
			<%if("yes".equals(showCommentIDString)){ %>
			<br>
			<%=file.getCommentIdString()==null?"":file.getCommentIdString() %>
			<%} %>
		</div>
		
		<div class="new_comment" id="new_comment">
			<%if(user==null) {%>
				<span id="new_comment_reply" class="gray">Reply</span>
				<span id="new_commnet_tip" class="hidden">Please Login to leave a reply!</span>
			<%}else{ %>
			<div class="input">
				<form action="/receive" method="post" enctype="multipart/form-data" onsubmit="return setHiddenValue();">
					<span class="article_author"><%=user==null?"":user.getNickname()%></span>
					<div class="new_comment_content">
						<textarea id="content" rows="8"></textarea>
						<div class="submit"><input type="submit" value="Submit"/></div>
					</div>
					<input type="hidden" name="method" value="recevieBlogComment"/>
					<input type="hidden" name="posturl" value="/blog/page/detail.jsp"/>
					<input type="hidden" name="blogID" value="<%=fileID%>"/>
					<input type="hidden" name="author"  class="commUnicode" value="<%=user==null?"":user.getNickname()%>">
					<input type="hidden" name="email" class="commUnicode" value="<%=user==null?"":user.getEmail()%>">
					<input type="hidden" name="title" value="0063006F006D006D0065006E007400200074006F0020300A<%=new String(file.getTitle()) %>300B">
					<textarea name="content" class="commUnicode hidden getValueFrom"></textarea>
				</form>
			</div>
			<%} %>
		</div>
		
	
	
</div>

<script type="text/javascript">
    var common = new Common();
    common.convertSafeStringClassElementInnerHtmlFromUnicodeToSafeString();
	// change the value of hidden input 
    function setHiddenValue(){
		// find the element with the classname 'getValueFrom'
		var list = document.getElementsByClassName("getValueFrom");
		var i = 0;
		for(i=0;i<list.length;i++){
			var e = list[i];
			if(!document.getElementById(e.name).value) {
				alert(e.name + " can not be empty!");
				return false;
			}
			e.value = document.getElementById(e.name).value;
		}
		
		// convert to unicode
		common.convertCommUnicodeClassInputValueFromStringToUnicode();
		
		//
		return true;
	}
	
	// load tip text
	document.getElementById("new_comment_reply").onmouseover = new_comment_mouseover;
	
	document.getElementById("new_commnet_tip").onmouseout = new_comment_mouseout;
	
	// for tip mouseover
	function new_comment_mouseover(){
		document.getElementById("new_comment_reply").className = "hidden";
		document.getElementById("new_commnet_tip").className = "inline";
		
	}
	
	
	function new_comment_mouseout(){
		document.getElementById("new_comment_reply").className = "inline gray";
		document.getElementById("new_commnet_tip").className = "hidden";
	}
	
</script>


<%} %>

<div class="south">
	<div class="footer">
		<iframe class="footer" src="/footer.html" frameborder="0" width="100%"></iframe>
	</div>
</div>
</body>
</html>